package com.ssis.core;

import java.util.EnumMap;
import com.ssis.graphics.Sprite;

public class Cell {
	public static enum CellType {
		EMPTY,
		FRAME,
		CARGO,
		DORM,
		QUARTERS,
		OFFICE,
		GENERATOR,
		CAPACITOR
	}
	private CellType type;
	private static EnumMap<CellType, Integer> typeFrames;
	private Sprite sprite;
	public static final int CELL_WIDTH = 64;
	public static final int CELLSHEET_ROWS = 4;
	public static final int CELLSHEET_COLS = 4;
	private Crew[] crew;
	private int x;
	private int y;
	
	public Cell(CellType type, int x, int y, int xOffset, int yOffset) {
		if(typeFrames == null)
			initTypeFrames();
		this.type = type;
		this.x = x;
		this.y = y;
		sprite = new Sprite("cellsheet", x * CELL_WIDTH - xOffset, y * CELL_WIDTH - yOffset, typeFrames.get(type), CELLSHEET_ROWS, CELLSHEET_COLS);
		initCrew();
	}
	
	private void initCrew() {
		if (type == CellType.DORM)
			crew = new Crew[2];
		else if (type == CellType.QUARTERS)
			crew = new Crew[4];
		else if (type == CellType.OFFICE)
			crew = new Crew[1];
		else
			crew = new Crew[0];
	}

	private void initTypeFrames() {
		typeFrames = new EnumMap<CellType, Integer>(CellType.class);
		typeFrames.put(CellType.EMPTY, 0);
		typeFrames.put(CellType.FRAME, 1);
		typeFrames.put(CellType.CARGO, 2);
		typeFrames.put(CellType.DORM, 3);
		typeFrames.put(CellType.QUARTERS, 4);
		typeFrames.put(CellType.OFFICE, 5);
		typeFrames.put(CellType.GENERATOR, 6);
		typeFrames.put(CellType.CAPACITOR, 7);
	}

	public void render() {
		sprite.render();
	}
	public CellType getType() {
		return type;
	}
	
	public void move(int x, int y) {
		sprite.translate(-x, -y);
	}

	public int income() {
		if (type == CellType.OFFICE) {
			return 5;
		}
		return 0;
	}
	
	public int powerGeneration() {
		if (type == CellType.GENERATOR)
			return 25;
		if (type == CellType.EMPTY)
			return 0;
		return -5;
	}
	
	public int powerCapacity() {
		if (type == CellType.CAPACITOR)
			return 100;
		return 0;
	}
	
	public int housingCapacity() {
		if (type == CellType.QUARTERS)
			return 4;
		if (type == CellType.DORM)
			return 2;
		return 0;
	}
	
	public int crewCapacity() {
		return crew.length;
	}
	
	public Crew getCrew(int index) {
		if(index < crew.length)
			return crew[index];
		return null;
	}
	
	public int crewSize() {
		int count = 0;
		for(int i=0; i<crew.length; i++)
			if (crew[i] != null)
				i++;
		return count;
	}

	public void removeCrew(int index) {
		crew[index] = null;
	}
	
	public void removeCrew(Crew member) {
		for(int i=0; i<crew.length; i++)
			if (crew[i] == member)
				crew[i] = null;
	}
	
	public void upgrade(CellType type) {
		this.type = type;
		sprite.changeFrame(typeFrames.get(type));
	}
	
	public String toString() {
		switch (type) {
		case EMPTY:		return x+","+y+","+"Empty";
		case FRAME:		return x+","+y+","+"Frame";
		case CARGO:		return x+","+y+","+"Cargo";
		case DORM:		return x+","+y+","+"Dorm";
		case QUARTERS:	return x+","+y+","+"Quarters";
		case OFFICE:	return x+","+y+","+"Office";
		case GENERATOR:	return x+","+y+","+"Generator";
		case CAPACITOR:	return x+","+y+","+"Capacitor";
		}
		return "Empty";
	}
}
